// import {
//     contentType,
//     requestTimeout
//   } from '@/config/net.config';
//   import { baseURL } from '@/config/net.config'


//@ts-ignore 
import { netConfig } from '@/config/net.config'

import axios from "axios";
//@ts-ignore
import { useUserStore } from '@/stores/modules/user'
//@ts-ignore 
import { useActivityStore } from '@/stores/modules/activity'
import { storeToRefs } from 'pinia'
const contentType = 'application/json;chartest=utf-8'
const requestTimeout = 10000
// const baseURL = 'https://test.jiahuier.com/'
//   const baseURL = 'https://www.jiahui5.com/'
const baseURL = netConfig.baseURL


/**
 * @description axios初始化
 */
const instance = axios.create({
    baseURL,
    timeout: requestTimeout,
    headers: {
        'Content-Type': contentType,
    },
});


/**
 * @description axios请求拦截器
 */
instance.interceptors.request.use((config) => {
    // 判断token是否存在
    // token && (config.headers.Authorization = `Bearer ${token}`)
    const userStore = useUserStore()
    const { token } = userStore

    const activityStore = useActivityStore()
    const { getActivityId } = storeToRefs(activityStore)



    if (token) config.headers['Authorization'] = `Bearer ${token}`
    if (getActivityId.value) config.headers['actid'] = getActivityId.value
    return config;
}, (error) => {
    return Promise.reject(error);
});


/**
 * @description axios响应拦截器
 */
instance.interceptors.response.use((res) => {
    const { status, data } = res
    switch (status) {
        case 200:
        case 201:
            return Promise.resolve(data)
        default:
            return Promise.reject(data)
    }
    // if (status == 200) {
    //     if (data) {
    //         return data
    //     } else {
    //         return Promise.reject(data)
    //     }
    // }
    // return Promise.reject(data)
}, (error) => {
    let { response } = error
    // let { message = 'Request Error', response } = error
    // const errMsg = get(response, 'data.message', message)
    // return Promise.reject(errMsg)
    return Promise.reject(response)
})

export default instance